<template>
  <div>
    <el-card>
      <h2>员工导入</h2>
      <UpdateExcel :before-upload="beforeUpload" :on-success="onSuccess" />
    </el-card>
  </div>
</template>

<script>
import { sysUserDatchAPI } from '@/api'
import moment from 'moment'
export default {
  data() {
    return {
      property: 'value'
    }
  },
  methods: {
    beforeUpload(obj) {
      return true
    },
    onSuccess(obj) {
      //  定义转换规则
      const temp = {
        入职日期: 'timeOfEntry',
        姓名: 'username',
        工号: 'workNumber',
        手机号: 'mobile',
        转正日期: 'correctionTime'
      }
      // 循环列表数据
      const newArr = obj.results.map((value) => {
        // 新对象
        const newObj = {}
        Object.keys(value).forEach((item) => {
          if (item == '入职日期' || item == '转正日期') {
            newObj[temp[item]] = this.changeDate(value[item]/** */)
          } else {
            newObj[temp[item]] = value[item]
          }
          console.log(newArr)
        })
        return newObj
      })
      sysUserDatchAPI(newArr)
      this.$router.push('/employees')
    },
    changeDate(time) {
      const t = new Date((time - 1) * 24 * 60 * 60 * 1000 - 8 * 60 * 60 * 1000)
      t.setYear(t.getFullYear() - 70)
      return moment(t).format('YYYY-MM-DD')
    }
  }
}
</script>

<style></style>
